//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.constans.Constant;
import com.wsd.smartcarwasher.util.FileUtil;
import com.wsd.smartcarwasher.util.ReturnUtil;
import com.wsd.smartcarwasher.util.StringUtil;
import java.io.File;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.multipart.MultipartFile;

@Service
public class FileService {
    private static final Logger logger = LoggerFactory.getLogger(FileService.class);

    public FileService() {
    }

    public ModelMap uploadFile(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        fileName = this.getFileName(fileName);

        try {
            File dest = FileUtil.createFile(System.getProperty("user.dir")+ File.separator+"smartwashImages"+File.separator+ fileName);
            file.transferTo(dest);
            String url = Constant.DOMAIN + fileName;
            return ReturnUtil.Success("文件上传成功", url);
        } catch (IOException var5) {
            logger.error("上传文件失败" + fileName, var5);
            return ReturnUtil.Error("文件上传失败");
        }
    }

    private String getFileName(String fileName) {
        String fileType = fileName.substring(fileName.lastIndexOf("."));

        File file;
        String newFileName;
        do {
            newFileName = StringUtil.getRandomUUID("") + fileType;
            file = new File(System.getProperty("user.dir")+ File.separator+"smartwashImages"+File.separator + newFileName);
        } while(file.exists());

        return newFileName;
    }
}


/*
package com.wsd.smartcarwasher.service;

import com.wsd.smartcarwasher.constans.Constant;
import com.wsd.smartcarwasher.domain.UserInfo;
import com.wsd.smartcarwasher.util.FileUtil;
import com.wsd.smartcarwasher.util.ReturnUtil;
import com.wsd.smartcarwasher.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@Service
public class FileService {
    private static final Logger logger = LoggerFactory.getLogger(FileService.class);

    @Resource
    private UserInfoService userInfoService;

    public ModelMap uploadFile(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        fileName = getFileName(fileName);
        String path = Constant.FILE_PATH_PREFIX  + "/" + fileName;

        try {
            File dest = FileUtil.createFile(Constant.FILE_PATH_PREFIX + fileName);
            //上传文件
            file.transferTo(dest); //保存文件
            String url = Constant.DOMAIN + fileName;//页面访问url
            return ReturnUtil.Success("文件上传成功", url);
        } catch (IOException e) {
            logger.error("上传文件失败" + path, e.getMessage());
            return ReturnUtil.Error("文件上传失败");
        }
    }

    //生成
    private String getFileName(String fileName) {
        File file;
        String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
        String newFileName;
        do {
            newFileName = StringUtil.getRandomUUID("") + fileType;
            file = new File(Constant.FILE_PATH_PREFIX + newFileName);
        } while (file.exists());
        return newFileName;
    }
}
*/
